#include "tool_pushbutton.h"
#include <QHBoxLayout>
#include <QGridLayout>
#include <QDebug>

ToolPushButton::ToolPushButton(QWidget *parent)
    : QWidget(parent)
{
    QHBoxLayout *MoveToolLayout = new QHBoxLayout(this); // 主布局

    ToolPtnWidget = new QWidget(this);
    ToolPtnWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    setupLayout(ToolPtnWidget);
    connectSignals(ToolPtnWidget);
    MoveToolLayout->setContentsMargins(0, 0, 0, 0);
    MoveToolLayout->addWidget(ToolPtnWidget);
    this->setLayout(MoveToolLayout);
}

void ToolPushButton::setupLayout(QWidget *widget)
{
    QHBoxLayout *layout = new QHBoxLayout(widget);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(5);

    // 创建按钮
    upButton = new QPushButton(widget);
    downButton = new QPushButton(widget);
    positionUpButton = new QPushButton(widget);
    positionDownButton = new QPushButton(widget);
    positionLeftButton = new QPushButton(widget);
    positionRightButton = new QPushButton(widget);
    LeftButton = new QPushButton(widget);
    RightButton = new QPushButton(widget);
    rotateLeftButton = new QPushButton(widget);
    rotateRightButton = new QPushButton(widget);
    rotateUpButton = new QPushButton(widget);
    rotateDownButton = new QPushButton(widget);

    QList<QPushButton*> allButtons = {
        upButton, downButton, positionUpButton, positionDownButton,
        positionLeftButton, positionRightButton, LeftButton, RightButton,
        rotateLeftButton, rotateRightButton, rotateUpButton, rotateDownButton
    };

    for (auto btn : allButtons)
        btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    QString baseStyle = R"(
    QPushButton {
            min-height: 70px;
            border: none;
            border-radius: 8px;
            font-size: 24px;
            font-weight: bold;
            color: white;
            qproperty-iconSize: 40px 40px;
            background-origin: border-box;
            margin-top: 0px;
        }
        QPushButton:hover {
            margin-top: -3px;
            border-radius: 8px; /* 保持圆角不变 */
        }
    )";

    // 设置样式 + 图标
    upButton->setStyleSheet(baseStyle + R"(
        QPushButton {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                         stop:0 #1976d2, stop:1 #0d47a1);
            qproperty-icon: url(:/img/arrow-up_line.png);
        })");

    downButton->setStyleSheet(baseStyle + R"(
        QPushButton {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                         stop:0 #0d47a1, stop:1 #1976d2);
            qproperty-icon: url(:/img/arrow-down_line.png);
        })");

    positionUpButton->setStyleSheet(baseStyle + R"(
        QPushButton {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                         stop:0 #1976d2, stop:1 #0d47a1);
            qproperty-icon: url(:/img/arrow-up_line.png);
        })");

    positionDownButton->setStyleSheet(baseStyle + R"(
        QPushButton {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                         stop:0 #0d47a1, stop:1 #1976d2);
            qproperty-icon: url(:/img/arrow-down_line.png);
        })");

    positionLeftButton->setStyleSheet(baseStyle + R"(
        QPushButton {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                         stop:0 #1e88e5, stop:1 #0d47a1);
            qproperty-icon: url(:/img/arrow-left_line.png);
        })");

    positionRightButton->setStyleSheet(baseStyle + R"(
        QPushButton {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                         stop:0 #0d47a1, stop:1 #1e88e5);
            qproperty-icon: url(:/img/arrow-right_line.png);
        })");

    rotateUpButton->setStyleSheet(baseStyle + R"(
        QPushButton {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                         stop:0 #1565c0, stop:1 #1e88e5);
            qproperty-icon: url(:/img/arrow-up_line.png);
        })");

    rotateDownButton->setStyleSheet(baseStyle + R"(
        QPushButton {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                         stop:0 #1e88e5, stop:1 #1565c0);
            qproperty-icon: url(:/img/arrow-down_line.png);
        })");

    rotateLeftButton->setStyleSheet(baseStyle + R"(
    QPushButton {
        background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                     stop:0 #1e88e5, stop:1 #0d47a1);
        qproperty-icon: url(:/img/arrow-left_line.png);
    })");

    rotateRightButton->setStyleSheet(baseStyle + R"(
    QPushButton {
        background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                     stop:0 #1e88e5, stop:1 #0d47a1);
        qproperty-icon: url(:/img/arrow-right_line.png);
    })");

    LeftButton->setStyleSheet(baseStyle + R"(
    QPushButton {
        background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                     stop:0 #1976d2, stop:1 #0d47a1);
        qproperty-icon: url(:/img/rotation_left_line.png);
    })");

    RightButton->setStyleSheet(baseStyle + R"(
    QPushButton {
        background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                     stop:0 #1976d2, stop:1 #0d47a1);
        qproperty-icon: url(:/img/rotation_right_line.png);
    })");


    // 创建标签和布局
    positionLabel = new QLabel("Position", widget);
    rotationLabel = new QLabel("Rotation", widget);

    QWidget *positionWidget = new QWidget(widget);
    QGridLayout *positionLayout = new QGridLayout(positionWidget);
    positionLayout->setContentsMargins(0, 0, 0, 0);
    positionLayout->setSpacing(0);
    positionLayout->addWidget(positionLabel, 2, 1, Qt::AlignCenter);
    positionLayout->addWidget(upButton, 0, 0);
    positionLayout->addWidget(downButton, 0, 2);
    positionLayout->addWidget(positionUpButton, 1, 1);
    positionLayout->addWidget(positionDownButton, 3, 1);
    positionLayout->addWidget(positionLeftButton, 2, 0);
    positionLayout->addWidget(positionRightButton, 2, 2);
    positionWidget->setLayout(positionLayout);

    QWidget *rotationWidget = new QWidget(widget);
    QGridLayout *rotationLayout = new QGridLayout(rotationWidget);
    rotationLayout->setContentsMargins(0, 0, 0, 0);
    rotationLayout->setSpacing(0);
    rotationLayout->addWidget(rotationLabel, 2, 1, Qt::AlignCenter);
    rotationLayout->addWidget(LeftButton, 0, 0);
    rotationLayout->addWidget(RightButton, 0, 2);
    rotationLayout->addWidget(rotateUpButton, 1, 1);
    rotationLayout->addWidget(rotateDownButton, 3, 1);
    rotationLayout->addWidget(rotateLeftButton, 2, 0);
    rotationLayout->addWidget(rotateRightButton, 2, 2);
    rotationWidget->setLayout(rotationLayout);

    layout->addWidget(positionWidget);
    layout->addWidget(rotationWidget);
    widget->setLayout(layout);
}

void ToolPushButton::connectSignals(QWidget *widget)
{
    QList<QPushButton*> buttons = {
        upButton, downButton, positionUpButton, positionDownButton,
        positionLeftButton, positionRightButton, LeftButton, RightButton,
        rotateLeftButton, rotateRightButton, rotateUpButton, rotateDownButton
    };
    for (auto btn : buttons) {
        //connect(btn, &QPushButton::clicked, this, &ToolPushButton::onButtonClicked);
        connect(btn, &QPushButton::pressed, this, &ToolPushButton::onButtonPressed);
        connect(btn, &QPushButton::released, this, &ToolPushButton::onButtonReleased);
    }
}

void ToolPushButton::setCommands(const QJsonObject &flag5Details)
{
    commandMap.clear();

    QMap<QString, QPushButton*> btnMap = {
                                           {"LeftButton", LeftButton},
                                           {"RightButton", RightButton},
                                           {"rotateLeftButton", rotateLeftButton},
                                           {"rotateRightButton", rotateRightButton},
                                           {"rotateUpButton", rotateUpButton},
                                           {"rotateDownButton", rotateDownButton},
                                           {"upButton", upButton},
                                           {"downButton", downButton},
                                           {"positionUpButton", positionUpButton},
                                           {"positionDownButton", positionDownButton},
                                           {"positionLeftButton", positionLeftButton},
                                           {"positionRightButton", positionRightButton},
                                           };

    for (auto it = flag5Details.begin(); it != flag5Details.end(); ++it) {
        QString key = it.key();
        // if (!btnMap.contains(key))
        //     continue;

        QJsonObject btnObj = it.value().toObject();
        QString cmd = btnObj.value("Commands").toString();
        QString name = btnObj.value("name").toString();


        if(name == "stopButton")
        {
            stopCommand = cmd;
            continue;
        }

        QPushButton *btn = btnMap.value(key);
        if (!btn)
            continue;

        btn->setObjectName(name);
        commandMap[name] = cmd;
    }
}

void ToolPushButton::onButtonClicked()
{
    QPushButton *btn = qobject_cast<QPushButton*>(sender());
    if (!btn)
        return;

    QString btnName = btn->objectName();
    if (!commandMap.contains(btnName)) {
        qWarning() << "Command not found for button:" << btnName;
        return;
    }

    QString cmd = commandMap.value(btnName);
    qDebug() << "Execute command for button" << btnName << ":" << cmd;
    qDebug() << "Execute Stop command for button:" <<stopCommand;
    emit commandTriggered(cmd);
}

void ToolPushButton::onButtonPressed()
{
    QPushButton *btn = qobject_cast<QPushButton*>(sender());
    if (!btn)
        return;

    QString btnName = btn->objectName();
    if (!commandMap.contains(btnName)) {
        qWarning() << "Command not found for button:" << btnName;
        return;
    }

    QString cmd = commandMap.value(btnName);
    // qDebug() << "Execute command for button" << btnName << ":" << cmd;

    emit commandTriggered(cmd);
}

void ToolPushButton::onButtonReleased()
{
    QPushButton *btn = qobject_cast<QPushButton*>(sender());
    if (!btn)
        return;

    QString btnName = btn->objectName();
    if (!commandMap.contains(btnName)) {
        qWarning() << "Command not found for button:" << btnName;
        return;
    }

    QString cmd = commandMap.value(btnName);
    // qDebug() << "Execute command for button" << btnName << ":" << cmd;
    // qDebug() << "Execute RElease command for button:" <<stopCommand;
    emit commandTriggered(stopCommand);
}

QWidget* ToolPushButton::getWidget()
{
    return this;
}
